<div id="kaoHeKaiLiaoYuan" style="height: 100%;width: 100%">

    <div  style="width:100%;text-align: right;background-color: #009E94;" >
        <input id="peiZhiCheJianFuZhuRenYuanAssessMonth" data-options="label:'考核月份',labelWidth:60,width:200" type= "text" class= "easyui-datebox" required ="required">
        <a id="fuZhuRenYuanSubmit" class="easyui-linkbutton" style="width: 80px;background-color:#a1af1a" plain=true><i class="fa fa-check-circle fa-lg fa-fw"></i>提交</a>
    </div>
    <div style="height:93%;">
        <!--奖金表格-->
        <table id="peiZhiCheJianFuZhuRenYuan"></table>
    </div>
    <div id="peiZhiCheJianFuZhuRenYuanToolbar" style="display: none;">
        <div style="width: 100px;float: left">
            <a class="easyui-linkbutton" data-options="plain:true" onclick="javaScript:$('#peiZhiCheJianFuZhuRenYuan').edatagrid('saveRow')"><i class="fa fa-question-circle fa-fw" aria-hidden="true"></i>说明</a>
        </div>
        <div style="width: 250px;float:right;text-align: right">
            <a class="easyui-linkbutton" data-options="plain:true" onclick="javaScript:$('#peiZhiCheJianFuZhuRenYuan').edatagrid('saveRow')"><i class="fa fa-floppy-o fa-fw" aria-hidden="true"></i>保存</a>
            <a class="easyui-linkbutton" data-options="plain:true">|</a>
            <a class="easyui-linkbutton" data-options="plain:true" onclick="javaScript:$('#peiZhiCheJianFuZhuRenYuan').edatagrid('destroyRow')"><i class="fa fa-trash fa-fw" aria-hidden="true"></i>删除</a>
            <a class="easyui-linkbutton" data-options="plain:true">|</a>
            <a class="easyui-linkbutton" data-options="plain:true" onClick="addpeiZhiCheJianFuZhuRenYuanEmp()"><i class="fa fa-plus-square fa-fw" aria-hidden="true"></i>添加</a>
        </div>
        <!--清除浮动-->
        <div style="clear: both"></div>
    </div>

    <form accept-charset=""></form>
    <script th:inline="none">
        //当前编辑行的索引
        var peiZhiCheJianFuZhuRenYuanIndex=0;

        //提交数据
        $("#fuZhuRenYuanSubmit").click(function () {
            var rows = $("#peiZhiCheJianFuZhuRenYuan").datagrid("getRows");
            rows.forEach(function (val) {
                val.assessYear=pzcj_currentYear;
                val.assessMonth=pzcj_currentMonth;
                val.assessDept=pzcj_currentDept;
            })
            var footerRows = $("#peiZhiCheJianFuZhuRenYuan").datagrid("getFooterRows");
            var data=JSON.stringify({jxAssessPzcjs:rows,maps:footerRows});
            var result = commonAjax("post","/jx/pzcj/addFuZhuRenYuanAssessData",data,"application/json;charset=utf-8");
            if (result.status==200){
                $("#peiZhiCheJianFuZhuRenYuan").datagrid("loadData",{total: 0, rows: [],footer:[]});
                windowsOperation(null,null,result)
            }else {
                layer.msg(result.msg,{"icon":5})
            }

        })

        //绩效考核表添加事件
        function addpeiZhiCheJianFuZhuRenYuanEmp(){

        }

        //选择月份加载月份数据
        $("#peiZhiCheJianFuZhuRenYuanAssessMonth").datebox({
            onSelect: function(date){
                $("#peiZhiCheJianFuZhuRenYuan").datagrid("loadData",[])
                var currentYear=date.getFullYear()
                var currentMonth=date.getMonth()+1
                fuZhuRenYuanInit(currentYear,currentMonth)
                addPZCJData("peiZhiCheJianFuZhuRenYuan",window.JX_PZCJ_FUZHURENYUAN,"1002O110000000000MMB",currentYear,currentMonth)
            }
        })

        //进页面默认调用当月数据
        var date= new Date();
        fuZhuRenYuanInit(date.getFullYear(),date.getMonth()+1)

//绩效考核表
        function fuZhuRenYuanInit(currentYear,currentMonth) {
            pzcj_currentYear=currentYear
            pzcj_currentMonth=currentMonth
            var currentYM=currentYear+"年"+currentMonth+"月";
            var fuZhuRenYuan = {
                init: [//表头
                    [
                        {"title": ' <span style="font-size: 18px">辅助人员综合绩效奖</span><span style="color: #009E94">(绩效奖金标准*出勤率-绩效扣款)</span>',"align": "center", "colspan":9},
                    ],
                    [   {"title": '考核部门（科室）:',"align": "center", "colspan":2},
                        {"field": 'id3', "title": "生产部/配制车间","align": "center", "colspan":2},
                        {"title": '', "align": "center","colspan":2},
                        {"title": '考核周期:',"align": "center"},
                        {"field": 'id2', "title": currentYM, "align": "center","colspan":1},
                        {"field": 'resultMoney', "title": '最终绩效<br/>金额（元）', "width": 30,"rowspan":2,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                    ],
                    [
                        {"field": 'id', "title": 'ID', "width": 0, "align": "center", "hidden": "true"},
                        {"field": 'empName', "title": '姓名', "width": 30, "align": "center"},
                        {"field": 'empCode', "title": '工号', "width": 30, "align": "center"},
                        {"field": 'postName',"title": '岗位',"width": 30,"align": "center"},
                        {"field": 'workshopEfficiency', "title": '车间<br/>生产效率', "width": 30, "align": "center","editor": {"type": "textbox", "options": {"onChange":peiZhiCheJianFuZhuRenYuanWorkshopEfficiency}}},
                        {"field": 'performanceBonus', "title": '绩效奖金<br/>标准', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                        {"field": 'deduct', "title": '绩效扣款', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":peiZhiCheJianFuZhuRenYuanAttendanceRateAndDeduct}}},
                        {"field": 'attendanceRate', "title": '出勤率(%)', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":peiZhiCheJianFuZhuRenYuanAttendanceRateAndDeduct}}},
                    ]]

            }

            //奖金基数表格
            $("#peiZhiCheJianFuZhuRenYuan").edatagrid({
                nowrap: false,
                columns: fuZhuRenYuan.init,
                showFooter: true,
                autoSave:true,
                fitColumns: true,
                scrollbarSize:0,  //最右侧多出来的去除
                toolbar:"#peiZhiCheJianFuZhuRenYuanToolbar",
                onBeforeEdit:function (index, row) { //编辑器打开触发事件
                    peiZhiCheJianFuZhuRenYuanIndex=index;//保存当前索引

                },
                onAfterEdit:function (index, row) {
                    console.log("在用户完成编辑一行的时候触发:统计最终综合绩效")
                    totalpeiZhiCheJianFuZhuRenYuan();
                },
                onDestroy:function (index,row) { //删除一行触发事件
                    totalpeiZhiCheJianFuZhuRenYuan();
                }

            })
        }

        //合计最终综合绩效
        function totalpeiZhiCheJianFuZhuRenYuan() {
            //添加页脚行方法
            //$("#peiZhiCheJianFuZhuRenYuan").edatagrid('reloadFooter', window.fuZhuRenYuanFooters)
            //console.log("统计全部--最终绩效奖金总合计")
            //统计
            var rows = $("#peiZhiCheJianFuZhuRenYuan").datagrid("getRows");
            var  result=0;
            rows.forEach(function (row) {
                var val=Number(row.resultMoney)
                if (val){
                    result+=val
                }

            })

            //添加页脚行方法
            $("#peiZhiCheJianFuZhuRenYuan").edatagrid('reloadFooter', [{
                empCode: "合计", resultMoney: fomatFloat(result,2)
            }])
        }

        //车间生产效率事件：为绩效标准赋值
        function peiZhiCheJianFuZhuRenYuanWorkshopEfficiency(newValue,oldValue){
            //console.log("车间生产效率事件")
            if (newValue){
                newValue = Number(newValue);
                //绩效奖金标准
                var performanceBonus = $('#peiZhiCheJianFuZhuRenYuan').datagrid('getEditor', {
                    index: peiZhiCheJianFuZhuRenYuanIndex,
                    field: 'performanceBonus'
                }).target;

                if(newValue<=95 || newValue>103){
                    performanceBonus.textbox("setValue",0);
                }else if((102<=newValue && newValue<=103) || (95<newValue && newValue<=99)){
                    performanceBonus.textbox("setValue",250);
                }else if((100<newValue && newValue<102) || (99<newValue  && newValue<100)){
                    performanceBonus.textbox("setValue",300);
                } else if(newValue==100){
                    performanceBonus.textbox("setValue",350);
                }else {
                    var workshopEfficiency= getEditObject("workshopEfficiency")
                    workshopEfficiency.textbox("setValue", "")
                    layer.msg("请输入正确的值")
                    return
                }

            }
        }

        //扣款绩效和出勤率触发事件
        function peiZhiCheJianFuZhuRenYuanAttendanceRateAndDeduct() {
            peiZhiCheJianFuZhuRenYuanresultMoney() //计算最终绩效的方法
        }

        //计算最终绩效的方法:绩效奖金标准*出勤率-绩效扣款
        function peiZhiCheJianFuZhuRenYuanresultMoney() {
            //为了以后有可能会更改字段的位置或添加问题，不使用获取编辑器对象数组的方法
            //console.log("计算最终绩效的方法")
            //绩效奖金标准
            var performanceBonus = $('#peiZhiCheJianFuZhuRenYuan').datagrid('getEditor', {
                index: peiZhiCheJianFuZhuRenYuanIndex,
                field: 'performanceBonus'
            }).target.textbox("getValue");

            //出勤率
            var attendanceRate = $('#peiZhiCheJianFuZhuRenYuan').datagrid('getEditor', {
                index: peiZhiCheJianFuZhuRenYuanIndex,
                field: 'attendanceRate'
            }).target.textbox("getValue");

            //绩效扣款
            var deduct = $('#peiZhiCheJianFuZhuRenYuan').datagrid('getEditor', {
                index: peiZhiCheJianFuZhuRenYuanIndex,
                field: 'deduct'
            }).target.textbox("getValue");

            // 获取最终绩效奖金编辑器对象
            var resultMoney = $('#peiZhiCheJianFuZhuRenYuan').datagrid('getEditor', {
                index: peiZhiCheJianFuZhuRenYuanIndex,
                field: 'resultMoney'
            }).target;
            resultMoney.textbox("setValue",fomatFloat(performanceBonus*attendanceRate/100-deduct,2));
        }

        //获取当前编辑行指定的jquery对象
        function getEditObject(fieldValue) {
            var j = $('#peiZhiCheJianFuZhuRenYuan').datagrid('getEditor', {
                index: peiZhiCheJianFuZhuRenYuanIndex,
                field: fieldValue
            }).target;
            return j
        }
    </script>
</div>